package com.xueyu.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.xueyu.model.BookType;
import com.xueyu.util.StringUtil;

/**
 * 图书类别DAO
 * @author xueyu
 *
 */
public class BookTypeDao {
	/**
	 * 图书类别添加
	 * @param con
	 * @param bookType
	 * @return 添加的记录数
	 * @throws Exception
	 */
	public int add(Connection con , BookType bookType)throws Exception{
		String sql = "insert into t_bookType values(null,?,?)";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, bookType.getBookTypeName());
		pstmt.setString(2, bookType.getBookTypeDesc());
		return pstmt.executeUpdate();
	}
	/**
	 * 查询图书类别集合，没有查询条件时查询全部
	 * @param con
	 * @param bookType
	 * @return 查询集合ResultSet
	 * @throws Exception
	 */
	public ResultSet list(Connection con , BookType bookType) throws Exception{
		StringBuffer sb = new StringBuffer("select * from t_bookType");
		if(StringUtil.isNotEmpty(bookType.getBookTypeName())){
			sb.append(" and bookTypeName like '%"+bookType.getBookTypeName()+"%'");
		}
		PreparedStatement pstmt = con.prepareStatement(sb.toString().replaceFirst("and", "where"));
		return pstmt.executeQuery();
	}
	/**
	 * 图书类别删除
	 * @param con
	 * @param bookType
	 * @return 删除的记录数
	 * @throws Exception
	 */
	public int delete(Connection con , BookType bookType) throws Exception{
		String sql = "delete from t_bookType where id = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setInt(1, bookType.getId());
		return pstmt.executeUpdate();
	}
	/**
	 * 图书类别更新
	 * @param con
	 * @param bookType
	 * @return 更新的记录数
	 * @throws Exception
	 */
	public int update(Connection con , BookType bookType)throws Exception{
		String sql = "update t_bookType set bookTypeName = ?,bookTypeDesc = ? where id = ?";
		PreparedStatement pstmt = con.prepareStatement(sql);
		pstmt.setString(1, bookType.getBookTypeName());
		pstmt.setString(2, bookType.getBookTypeDesc());
		pstmt.setInt(3, bookType.getId());
		return pstmt.executeUpdate();
	}
	
}
